import axios from 'axios'

export default {
  namespaced: true,
  state: {
    // token: localStorage.getItem('token') || ''
    token: '',
    userInfo: {}
  },
  mutations: {
    // 更新 token  两个参数 state payload
    updataToken(state, payload) {
      state.token = payload
      // 将 token 存储到本地存储 持久化
      // localStorage.setItem('token', payload)
    },
    updataUserInfo(state, userInfo) {
      state.userInfo = userInfo
    }

  },
  actions: {
    // actions 第一个参数是 context
    async getUserInfo(context) {
      const { data: res } = await axios.get('/my/userinfo')
      // actions 不能直接向state传数据   通过mutations
      if (res.code !== 0) return
      context.commit('updataUserInfo', res.data)
    }

  },
  getters: {}
}
